﻿using System;
using System.Threading;
using ZFY.Core;
using ZFY.UnitOfWork.Interface;

namespace ZFY.UnitOfWork
{
    public class AmbientUnitOfWork : IAmbientUnitOfWork, ISignleDependency
    {
        public IZFYUnitOfWork UnitOfWork => _currentUow.Value;

        public Guid Id { get; set; }

        private readonly AsyncLocal<IZFYUnitOfWork> _currentUow;

        public AmbientUnitOfWork()
        {
            Id = Guid.NewGuid();
            _currentUow = new AsyncLocal<IZFYUnitOfWork>();
        }

        public void SetUnitOfWork(IZFYUnitOfWork unitOfWork)
        {
            _currentUow.Value = unitOfWork;
        }
    }
}
